package de.ttf.ts.ctx;

import javax.persistence.PersistenceException;

import org.hibernate.HibernateException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;

public class TtsPersistenceExceptionTranslator implements PersistenceExceptionTranslator {

    @Override
    public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
        if (ex instanceof HibernateException) {
            return SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex);
        }
        if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
            return SessionFactoryUtils.convertHibernateAccessException((HibernateException) ex.getCause());
        }
        return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
    }

}
